﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using System.Web.UI;
using BLL;
using Model;
using Model.RF.ProductCategory;
using BLL.RF.ProductCategory;

public partial class ProductManage_RFProductManage_Manage_ProductType_FlowManage : System.Web.UI.Page
{
    public ProductType_Flow pf;
    public string seachFactor
    {
        get { return ViewState["SeachFactor"].ToString(); }
        set { ViewState["SeachFactor"] = value; }
    }

    public ProductManage_RFProductManage_Manage_ProductType_FlowManage()
    {
        pf = new ProductType_Flow();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            seachFactor = string.Empty;
            BindProductTypeFlow();
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        seachFactor = this.TextBox1.Text.Trim();
        BindProductTypeFlow();
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        seachFactor = string.Empty;
        BindProductTypeFlow();
    }

    public void BindProductTypeFlow()
    {
        if (string.IsNullOrEmpty(seachFactor))
        {
            this.GridView1.DataSource = pf.LoadEntities();
            this.GridView1.DataBind();
        }
        else
        {
            IList<ProductType_FlowInfo> list = new List<ProductType_FlowInfo>();
            list.Add(pf.LoadEntity(seachFactor));
            this.GridView1.DataSource = list;
            this.GridView1.DataBind();
        }        
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GridView1.PageIndex = e.NewPageIndex;
        BindProductTypeFlow();
    }

    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        this.GridView1.EditIndex = -1;
        BindProductTypeFlow();
    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        this.GridView1.EditIndex = e.NewEditIndex;
        BindProductTypeFlow();
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //if (e.Row.RowType == DataControlRowType.DataRow)
        //{
        //    if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
        //    {
        //        for (int i = 2; i <= 8; i++)
        //        {
        //            if (e.Row.Cells[i].Text == "True")
        //                e.Row.Cells[i].Text = "是";
        //            e.Row.Cells[i].Text = "否";
        //        }
        //    }
        //}
        if ((((DropDownList)e.Row.FindControl("DDLAssembleStep")) != null) && (((DropDownList)e.Row.FindControl("DDLJointingStep")) != null)
            && (((DropDownList)e.Row.FindControl("DDLAgingStep")) != null)
            && (((DropDownList)e.Row.FindControl("DDLDebugATEStep")) != null) && (((DropDownList)e.Row.FindControl("DDLDebugStep")) != null)
            && (((DropDownList)e.Row.FindControl("DDLQCFixedStep")) != null) && (((DropDownList)e.Row.FindControl("DDLPIMStep")) != null)
            && (((DropDownList)e.Row.FindControl("DDLPowerStep")) != null) && (((DropDownList)e.Row.FindControl("DDLDebugActiveATEStep")) != null)
            && (((DropDownList)e.Row.FindControl("DDLVoltageCheckStep")) != null) && (((DropDownList)e.Row.FindControl("DDLCurrentCheckStep")) != null) 
            && (((DropDownList)e.Row.FindControl("DDLSensitivityStep")) != null) && (((DropDownList)e.Row.FindControl("DDLATEStep")) != null)
            && (((DropDownList)e.Row.FindControl("DDLActiveATEStep")) != null) && (((DropDownList)e.Row.FindControl("DDLOIP3Step")) != null) 
            && (((DropDownList)e.Row.FindControl("DDLHFATEStep")) != null)
            && (((DropDownList)e.Row.FindControl("DDLAirtightnessStep")) != null) && (((DropDownList)e.Row.FindControl("DDLPIMSecondStep")) != null)
            && (((DropDownList)e.Row.FindControl("DDLPackCheckStep")) != null) && (((DropDownList)e.Row.FindControl("DDLPackageStep")) != null))
        {
            ((DropDownList)e.Row.FindControl("DDLAssembleStep")).SelectedValue = ((HiddenField)e.Row.FindControl("HDFAssembleStep")).Value.Trim();
            ((DropDownList)e.Row.FindControl("DDLJointingStep")).SelectedValue = ((HiddenField)e.Row.FindControl("HDFJointingStep")).Value.Trim();
            ((DropDownList)e.Row.FindControl("DDLAgingStep")).SelectedValue = ((HiddenField)e.Row.FindControl("HDFAgingStep")).Value.Trim();
            ((DropDownList)e.Row.FindControl("DDLDebugATEStep")).SelectedValue = ((HiddenField)e.Row.FindControl("HDFDebugATEStep")).Value.Trim();
            ((DropDownList)e.Row.FindControl("DDLDebugStep")).SelectedValue = ((HiddenField)e.Row.FindControl("HDFDebugStep")).Value.Trim();
            ((DropDownList)e.Row.FindControl("DDLQCFixedStep")).SelectedValue = ((HiddenField)e.Row.FindControl("HDFQCFixedStep")).Value.Trim();
            ((DropDownList)e.Row.FindControl("DDLPIMStep")).SelectedValue = ((HiddenField)e.Row.FindControl("HDFPIMStep")).Value.Trim();
            ((DropDownList)e.Row.FindControl("DDLPowerStep")).SelectedValue = ((HiddenField)e.Row.FindControl("HDFPowerStep")).Value.Trim();
            ((DropDownList)e.Row.FindControl("DDLDebugActiveATEStep")).SelectedValue = ((HiddenField)e.Row.FindControl("HDFDebugActiveATEStep")).Value.Trim();
            ((DropDownList)e.Row.FindControl("DDLVoltageCheckStep")).SelectedValue = ((HiddenField)e.Row.FindControl("HDFVoltageCheckStep")).Value.Trim();
            ((DropDownList)e.Row.FindControl("DDLCurrentCheckStep")).SelectedValue = ((HiddenField)e.Row.FindControl("HDFCurrentCheckStep")).Value.Trim();
            ((DropDownList)e.Row.FindControl("DDLSensitivityStep")).SelectedValue = ((HiddenField)e.Row.FindControl("HDFSensitivityStep")).Value.Trim();
            ((DropDownList)e.Row.FindControl("DDLATEStep")).SelectedValue = ((HiddenField)e.Row.FindControl("HDFATEStep")).Value.Trim();
            ((DropDownList)e.Row.FindControl("DDLActiveATEStep")).SelectedValue = ((HiddenField)e.Row.FindControl("HDFActiveATEStep")).Value.Trim();
            ((DropDownList)e.Row.FindControl("DDLOIP3Step")).SelectedValue = ((HiddenField)e.Row.FindControl("HDFOIP3Step")).Value.Trim();
            ((DropDownList)e.Row.FindControl("DDLHFATEStep")).SelectedValue = ((HiddenField)e.Row.FindControl("HDFHFATEStep")).Value.Trim();
            ((DropDownList)e.Row.FindControl("DDLAirtightnessStep")).SelectedValue = ((HiddenField)e.Row.FindControl("HDFAirtightnessStep")).Value.Trim();
            ((DropDownList)e.Row.FindControl("DDLPIMSecondStep")).SelectedValue = ((HiddenField)e.Row.FindControl("HDFPIMSecondStep")).Value.Trim();
            ((DropDownList)e.Row.FindControl("DDLPackCheckStep")).SelectedValue = ((HiddenField)e.Row.FindControl("HDFPackCheckStep")).Value.Trim();
            ((DropDownList)e.Row.FindControl("DDLPackageStep")).SelectedValue = ((HiddenField)e.Row.FindControl("HDFPackageStep")).Value.Trim();
        }
    }

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        ProductType_FlowInfo pfi = new ProductType_FlowInfo();
        pfi.ProductTypeID = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
        pfi.ProductTypeName = GridView1.DataKeys[e.RowIndex].Values[1].ToString();
        pfi.AssembleStep = bool.Parse(((DropDownList)(GridView1.Rows[e.RowIndex].FindControl("DDLAssembleStep"))).SelectedValue);
        pfi.JointingStep = bool.Parse(((DropDownList)(GridView1.Rows[e.RowIndex].FindControl("DDLJointingStep"))).SelectedValue);
        pfi.AgingStep = bool.Parse(((DropDownList)(GridView1.Rows[e.RowIndex].FindControl("DDLAgingStep"))).SelectedValue);
        pfi.DebugATEStep = bool.Parse(((DropDownList)(GridView1.Rows[e.RowIndex].FindControl("DDLDebugATEStep"))).SelectedValue);
        pfi.DebugStep = bool.Parse(((DropDownList)(GridView1.Rows[e.RowIndex].FindControl("DDLDebugStep"))).SelectedValue);
        pfi.QCFixedStep = bool.Parse(((DropDownList)(GridView1.Rows[e.RowIndex].FindControl("DDLQCFixedStep"))).SelectedValue);
        pfi.PIMStep = bool.Parse(((DropDownList)(GridView1.Rows[e.RowIndex].FindControl("DDLPIMStep"))).SelectedValue);
        pfi.PowerStep = bool.Parse(((DropDownList)(GridView1.Rows[e.RowIndex].FindControl("DDLPowerStep"))).SelectedValue);
        pfi.DebugActiveATEStep = bool.Parse(((DropDownList)(GridView1.Rows[e.RowIndex].FindControl("DDLDebugActiveATEStep"))).SelectedValue);
        pfi.VoltageCheckStep = bool.Parse(((DropDownList)(GridView1.Rows[e.RowIndex].FindControl("DDLVoltageCheckStep"))).SelectedValue);
        pfi.CurrentCheckStep = bool.Parse(((DropDownList)(GridView1.Rows[e.RowIndex].FindControl("DDLCurrentCheckStep"))).SelectedValue);
        pfi.SensitivityStep = bool.Parse(((DropDownList)(GridView1.Rows[e.RowIndex].FindControl("DDLSensitivityStep"))).SelectedValue);
        pfi.ATEStep = bool.Parse(((DropDownList)(GridView1.Rows[e.RowIndex].FindControl("DDLATEStep"))).SelectedValue);
        pfi.ActiveATEStep = bool.Parse(((DropDownList)(GridView1.Rows[e.RowIndex].FindControl("DDLActiveATEStep"))).SelectedValue);
        pfi.OIP3Step = bool.Parse(((DropDownList)(GridView1.Rows[e.RowIndex].FindControl("DDLOIP3Step"))).SelectedValue);
        pfi.HFATEStep = bool.Parse(((DropDownList)(GridView1.Rows[e.RowIndex].FindControl("DDLHFATEStep"))).SelectedValue);
        pfi.AirtightnessStep = bool.Parse(((DropDownList)(GridView1.Rows[e.RowIndex].FindControl("DDLAirtightnessStep"))).SelectedValue);
        pfi.PIMSecondStep = bool.Parse(((DropDownList)(GridView1.Rows[e.RowIndex].FindControl("DDLPIMSecondStep"))).SelectedValue);  
        pfi.PackCheckStep = bool.Parse(((DropDownList)(GridView1.Rows[e.RowIndex].FindControl("DDLPackCheckStep"))).SelectedValue);  
        pfi.PackageStep = bool.Parse(((DropDownList)(GridView1.Rows[e.RowIndex].FindControl("DDLPackageStep"))).SelectedValue);

        //生成更改日志相关信息
        BuildandInsert_ProductType_FlowInfo(pfi);

        pf.Update(pfi);
        GridView1.EditIndex = -1;
        BindProductTypeFlow();
    }

    private void BuildandInsert_ProductType_FlowInfo(ProductType_FlowInfo pfi)
    {
        ProductType_FlowInfo oldpfi = pf.LoadEntity(pfi.ProductTypeID);
        ProductType_Flow_ChangeLogInfo pfci = new ProductType_Flow_ChangeLogInfo()
        {
            ID = 0,
            RecordTime = DateTime.Now,
            UserID = User.Identity.Name,
            IPAddress = IPGet.getIp(),
            ProductTypeID = pfi.ProductTypeID,
            OldStatus = "物料装配工序:" + GetStringbyBool(oldpfi.AssembleStep) + ", 腔内焊点检验工序:" + GetStringbyBool(oldpfi.JointingStep)
            + ", 时效工序:" + GetStringbyBool(oldpfi.AgingStep)
            + ", 调试ATE工序:" + GetStringbyBool(oldpfi.DebugATEStep) + ", 调试工序:" + GetStringbyBool(oldpfi.DebugStep)
            + ", QC定点焊点检验工序:" + GetStringbyBool(oldpfi.QCFixedStep) + ", 交调工序:" + GetStringbyBool(oldpfi.PIMStep)
            + ", 大功率测试工序:" + GetStringbyBool(oldpfi.PowerStep) + ", 调试有源ATE工序:" + GetStringbyBool(oldpfi.DebugActiveATEStep)
            + ", 电压检测工序:" + GetStringbyBool(oldpfi.VoltageCheckStep) + ", 电流检测工序:" + GetStringbyBool(oldpfi.CurrentCheckStep) 
            + ", 灵敏度测试工序:" + GetStringbyBool(oldpfi.SensitivityStep) + ", ATE测试工序:" + GetStringbyBool(oldpfi.ATEStep)
            + ", 有源ATE测试工序:" + GetStringbyBool(oldpfi.ActiveATEStep) + ", OIP3测试工序:" + GetStringbyBool(oldpfi.OIP3Step)
            + ", 高频电性能测试工序:" + GetStringbyBool(oldpfi.HFATEStep)
            + ", 气密测试工序:" + GetStringbyBool(oldpfi.AirtightnessStep) + ", 二次交调工序:" + GetStringbyBool(oldpfi.PIMSecondStep)
            + ", 包装前外观检验工序:" + GetStringbyBool(oldpfi.PackCheckStep) + ", 包装工序:" + GetStringbyBool(oldpfi.PackageStep),
            NewStatus = "物料装配工序:" + GetStringbyBool(pfi.AssembleStep) + ", 腔内焊点检验工序:" + GetStringbyBool(pfi.JointingStep)
            + ", 时效工序:" + GetStringbyBool(pfi.AgingStep)
            + ", 调试ATE工序:" + GetStringbyBool(pfi.DebugATEStep) + ", 调试工序:" + GetStringbyBool(pfi.DebugStep)
            + ", 定点焊点检验工序:" + GetStringbyBool(pfi.QCFixedStep) + ", 交调工序:" + GetStringbyBool(pfi.PIMStep)
            + ", 大功率测试工序:" + GetStringbyBool(pfi.PowerStep) + ", 调试有源ATE工序:" + GetStringbyBool(pfi.DebugActiveATEStep)
            + ", 电压检测工序:" + GetStringbyBool(pfi.VoltageCheckStep) + ", 电流检测工序:" + GetStringbyBool(pfi.CurrentCheckStep) 
            + ", 灵敏度测试工序:" + GetStringbyBool(oldpfi.SensitivityStep) + ", ATE测试工序:" + GetStringbyBool(pfi.ATEStep)
            + ", 有源ATE测试工序:" + GetStringbyBool(pfi.ActiveATEStep) + ", OIP3测试工序:" + GetStringbyBool(oldpfi.OIP3Step)
            + ", 高频电性能测试工序:" + GetStringbyBool(pfi.HFATEStep)
            + ", 气密测试工序:" + GetStringbyBool(pfi.AirtightnessStep) + ", 二次交调工序:" + GetStringbyBool(pfi.PIMSecondStep)
            + ", 包装前外观检验工序:" + GetStringbyBool(pfi.PackCheckStep) + ", 包装工序:" + GetStringbyBool(pfi.PackageStep),
        };
        if (!pfci.OldStatus.Equals(pfci.NewStatus))
        {
            ProductType_Flow_ChangeLog pfc = new ProductType_Flow_ChangeLog();
            pfc.Insert(pfci);
        }
    }

    private string GetStringbyBool(bool b)
    {
        return b == true ? "是" : "否";
    }
}
